package com.cn.wanxi.servlet.user;

import com.alibaba.fastjson.JSONObject;
import com.cn.wanxi.model.PageModel;
import com.cn.wanxi.model.UserModel;
import com.cn.wanxi.service.IUserService;
import com.cn.wanxi.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/back/user/findAll")
public class UserFindAllServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        1.乱码
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json; charset=utf-8");
//        2.拿到页面的值
        //        拿值
        String username = req.getParameter("username");
        String phone = req.getParameter("phone");
        String sex = req.getParameter("sex");
        String startBirthday = req.getParameter("startBirthday");
        String endBirthday = req.getParameter("endBirthday");
        String hobby = req.getParameter("hobby");
        hobby = hobby.replace("[", "").replace("]", "").replace(" ", "");
        hobby = hobby.replace("\"", "");
        String introduce = req.getParameter("introduce");
        String pageNum = req.getParameter("pageNum");
        String pageSize = req.getParameter("pageSize");

//        3.封装model
//        封装
        UserModel userModel = new UserModel();
        userModel.setUsername(username);
        userModel.setPhone(phone);
        userModel.setSex(sex);
        userModel.setStartBirthday(startBirthday);
        userModel.setEndBirthday(endBirthday);
        userModel.setHobby(hobby);
        userModel.setIntroduce(introduce);

        PageModel pageModel = new PageModel();
        pageModel.setPageNum(pageNum == null || "".equals(pageNum) ? 1 : Integer.parseInt(pageNum));
        pageModel.setPageSize(pageSize == null || "".equals(pageSize) ? 10 : Integer.parseInt(pageSize));
        userModel.setPageModel(pageModel);
//        4.调用服务逻辑层
        IUserService iUserService = new UserServiceImpl();
//        5.接受返回值
        List<UserModel> list = iUserService.findAll(userModel);
        int count=iUserService.getCount(userModel);
//        6.返回json（页面)
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list", list);//将整型转换为json对象
        jsonObject.put("count",count);
        resp.getWriter().println(jsonObject);//传递到页面的值
    }


    /**
     * 1.查询用户信息：将table换成div
     * 2.写代码的路径----html--css-js  servlet- iservice- serviceimpl-dao-impl-jdbc
     *  3.js 字符串拼接 for 公用的myajax
     *  4.编辑 页面传值问题
     *  5.查询要将值赋值到编辑页面   单选和多选赋值
     *  6.删除
     */
    /**
     * 分页修改
     * 1.在servlet里面增加
     *  String pageNum = req.getParameter("pageNum");
     *         String pageSize = req.getParameter("pageSize");
     *
     *          PageModel pageModel = new PageModel();
     *         pageModel.setPageNum(pageNum == null || "".equals(pageNum) ? 0 : Integer.parseInt(pageNum));
     *         pageModel.setPageSize(pageSize == null || "".equals(pageSize) ? 0 : Integer.parseInt(pageSize));
     *         userModel.setPageModel(pageModel);
     *  2.在model里面增加
     *   private PageModel pageModel;
     *  3.在数据实现层增加
     *  sql.append(" limit ").append((userModel.getPageModel().getPageNum() + -1) * userModel.getPageModel().getPageSize()).append(",").append(userModel.getPageModel().getPageSize());
     *
     *
     *   4.前台js代码
     */
}
